class ZhishisController < ApplicationController
  
  HUMANIZED_ATTRIBUTES = {
  :title => "标题"
  }
  
  def self.human_attribute_name(attr)
    HUMANIZED_ATTRIBUTES[attr.to_sym] || super
  end
  
  #validates_length_of :title, :in => 2..30, :message => '标题长度必须大于2个字符小于30个字符'
  #before_filter :authenticate, :except => [:index, :show]
  
  
  # GET /zhishis
  # GET /zhishis.xml
  def index
    @zhishis = Zhishi.find(:all,:order =>"id asc")
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @zhishis }
    end
  end
  
  def find_by_user
    @zhishis = Zhishi.find(:all,:limit=>3, :conditions =>["username=?",params[:username]],:order =>"id asc")
    render :action=>"index"
  end
  
  # GET /zhishis/1
  # GET /zhishis/1.xml
  def show
    @zhishi = Zhishi.find(params[:id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @zhishi }
    end
  end
  
  # GET /zhishis/new
  # GET /zhishis/new.xml
  def new
    @zhishi = Zhishi.new
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @zhishi }
    end
  end
  
  # GET /zhishis/1/edit
  def edit
    @zhishi = Zhishi.find(params[:id])
  end
  
  # POST /zhishis
  # POST /zhishis.xml
  def create
    @zhishi = Zhishi.new(params[:zhishi])    
    
    if !is_logged_in?
      flash[:error] = '用户没有登录!'
      redirect_to accounts_path #登录页面
    else
      @zhishi.username=logged_in_user.username
    end
    if @zhishi.save
      flash[:notice] = '知识已经成功保存.'
      redirect_to(@zhishi)
    else
      flash[:error] = '发生错误，数据没有保存!'
      render :action => "new"
    end
  end
    
  # PUT /zhishis/1
  # PUT /zhishis/1.xml
  def update
    @zhishi = Zhishi.find(params[:id])
    
    respond_to do |format|
      if @zhishi.update_attributes(params[:zhishi])
        flash[:notice] = 'Zhishi was successfully updated.'
        format.html { redirect_to(@zhishi) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @zhishi.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /zhishis/1
  # DELETE /zhishis/1.xml
  def destroy
    @zhishi = Zhishi.find(params[:id])
    @zhishi.destroy
    
    respond_to do |format|
      format.html { redirect_to(zhishis_url) }
      format.xml  { head :ok }
    end
  end
end
